package com.sowka.miko.weather;

import org.eclipse.osgi.framework.console.CommandInterpreter;
import org.eclipse.osgi.framework.console.CommandProvider;
import org.osgi.service.component.ComponentContext;

// referenced in component.xml
public class WeatherComponent implements CommandProvider {

	private ComponentContext context;

	private WeatherService service;

	public void _weather(CommandInterpreter ci) {
		String arg = ci.nextArgument();
		String word = ci.nextArgument();
		if (arg == null) {
			return;
		}
		if (arg.equalsIgnoreCase("fetch")) {
			ci.println(service.fetchForecast().toString());
		}
		if (arg.equalsIgnoreCase("providers")) {
			String[] providers = service.listProviders();
			ci.println("Registered providers:");
			for (String provider : providers) {
				ci.println("\t" + provider);
			}
		}
	}

	@Override
	public String getHelp() {
		StringBuilder buffer = new StringBuilder();
		buffer.append("---Miko Weather Service---\n");
		buffer.append("\tweather fetch - fetch the weather conditions from deafult provider\n");
		buffer.append("\tweather providers - list the registered providers\n");
		return buffer.toString();
	}

	public void activate(ComponentContext context) {
		this.context = context;
	}

	public void deactivate() {
		this.context = null;
	}

	public void bindWeather(WeatherService service) {
		this.service = service;
	}

	public void unbindWeather(WeatherService service) {
		this.service = null;
	}

}